<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {

    /**
	 * @var string userid
	 */
    private $_id;
    
    /**
	 * @var static string hashkey for mix with password before encrypt
	 */
    private static $hashkey = 'today is a gift';
    
    /**
	 * @var string login field name for username login
	 */
    private $loginfield = 'email';
    
    /**
	 * @var static string hashkey for mix with password before encrypt
	 */
    private $model = 'MemberModel';

    /**
     * Authenticates a user.
     * @return boolean whether authentication succeeds.
     */
    public function authenticate() {
        //$model  = $this->model;
        //$member = MemberModel::model()->find('email=:login', array(':login' => $this->username,));
        
        if ($this->username != "veniceq")
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        else if ($this->password != "ve120245")
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        else {
            $this->_id = 1;
            $this->username = "veniceq" ;
            $this->errorCode = self::ERROR_NONE;
        }
        return $this->errorCode == self::ERROR_NONE;
    }

    /**
     * @return integer the ID of the user record
     */
    public function getId() {
        return $this->_id;
    }

    /**
     * Generates the password hash.
     * 
     * @return string hash
     */
    public function hashPassword($password) {
        return sha1($password . self::$hashkey);
    }
    
    public function getAccessUser($level) {
       
        $criteria = new CDbCriteria;
        $criteria->select = 'email';
        $criteria->condition = 'level=:level';
        $criteria->params = array(':level' => $level,);

        $val = MemberModel::model()->findAll($criteria);
        $stack =array();
        foreach ($val as $email){
            array_push($stack, $email->email);
        }
        
        return $stack;
    }

}
